<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 头部搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="orderSerch">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderList" border stipe>
        <el-table-column type='index' label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send">
          <template slot-scope="scope">
            {{scope.row.is_send}}
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- <template slot-scope="scope"> -->
          <el-button size="mini" type="primary" icon="el-icon-edit" circle @click="showBox"></el-button>
          <el-button size="mini" type="success" icon="el-icon-location" circle @click="showLogisticsDialog"></el-button>
          <!-- </template> -->
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 订单编辑对话框 -->
    <el-dialog title="修改地址" :visible.sync="ressDialogVisible" width="50%" @close=ressDialogVisibleClose>
      <!-- 表单区域 -->
      <el-form ref="ressFormRef" :rules='ressFormRouls' :model="ressFormList" label-width="80px">
        <el-form-item label="省市区/县" prop="ress1">
          <el-cascader v-model="ressFormList.ress1" :options="citydata"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="ress2">
          <el-input v-model="ressFormList.ress2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息对话框 -->
    <el-dialog title="物流进度" :visible.sync="logisticsDialogVisible" width="50%">
      <el-timeline :reverse="reverse">
        <el-timeline-item v-for="(activity, index) in logisticsInfo" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata'
export default {
  data () {
    return {
      // 查询订单数据
      orderSerch: '',
      // 获取订单列表请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单总数
      total: 0,
      // 订单列表
      orderList: [],
      // 编辑对话框的展示
      ressDialogVisible: false,
      // 表单数据
      ressFormList: {
        ress1: [],
        ress2: ''
      },
      // 编辑表单验证规则
      ressFormRouls: {
        ress1: [{ required: true, message: '请输入省市区/县', trigger: 'blur' }],
        ress2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      citydata,
      // 物流对话框展示
      logisticsDialogVisible: false,
      // 物流信息数组
      logisticsInfo: [],
      // 物流时间线是否反向
      reverse: true
    }
  },
  methods: {
    // 获取订单列表
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      console.log(res)

      if (res.meta.status !== 200) return this.$Message.eroor(res.meta.meg)
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    // 监听每页的订单数量变化
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听当前的所在页
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getOrderList()
    },
    // 展示编辑对话框
    showBox () {
      this.ressDialogVisible = true
    },
    // 对话框关闭事件
    ressDialogVisibleClose () {
      // 清空表单
      this.$refs.ressFormRef.resetFields()
    },
    // 点击按钮弹出物流对话框
    async showLogisticsDialog () {
      // 获取物流信息
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')

      if (res.meta.status !== 200) return this.$Message.error(res.meta.msg)

      this.logisticsInfo = res.data
      console.log(this.logisticsInfo)
      this.logisticsDialogVisible = true
    }
  },
  created () {
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
</style>
